package org.yu.team2023.flashdays;


import lombok.*;
import net.minecraft.nbt.CompoundTag;

import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.capabilities.AutoRegisterCapability;
import net.minecraftforge.common.util.INBTSerializable;


import java.util.*;

@RequiredArgsConstructor
@AutoRegisterCapability
@EqualsAndHashCode
@ToString

public class PlayerRPGCapability implements INBTSerializable<CompoundTag>{
    //TODO 同步key
    final Player player;
    private final Map<String, Integer> keyMap =  Collections.synchronizedMap(new HashMap<>());



    public void removeKey(String key) {
        synchronized (keyMap) {
            if (!keyMap.containsKey(key)) return;
            int value = keyMap.get(key);
            if(value==2)
                SetTiger.getOrDefault(key).onUnformedTwo(player);
            if(value==4)
                SetTiger.getOrDefault(key).onUnformedFour(player);
            if (value > 1) keyMap.put(key, value - 1);
            else keyMap.remove(key);
        }
    }





    public void addKey(String key) {
        synchronized (keyMap) {
            keyMap.put(key, keyMap.getOrDefault(key, 0) + 1);
            int count=getKeyCount(key);
            if(count==2)
                SetTiger.getOrDefault(key).onFormedTwo(player);
            if(count==4)
                SetTiger.getOrDefault(key).onFormedFour(player);
        }
    }



    public boolean hasKey(String key) {
        synchronized (keyMap) {
            return keyMap.containsKey(key);
        }
    }



    public boolean isKeyOver2(String key) {
        synchronized (keyMap) {
            return getKeyCount(key) >= 2;
        }
    }

    public int getKeyCount(String key) {
        synchronized (keyMap) {
            return keyMap.getOrDefault(key, 0);
        }
    }


    protected void tick() {

    }







    @Override
    public CompoundTag serializeNBT() {
        CompoundTag compoundTag = new CompoundTag();


        return compoundTag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {


    }



    public boolean isKeyOver4(String setName) {
        return getKeyCount(setName)>=4;
    }

}
